﻿$.ajaxSetup({ url: "AuthorityRoleAdd.aspx", type: "post", dataType: "json" });

function BindAuthorityItems(flag) {
    $.ajax({
        data: { method: "GetAuthorityItems" },
        success: function (json) {
            $("#ul_Authority").append("#full_Authority", json.AuthorityItems);
            if (flag) {
                SetAuthority();
            }
        }
    });
}

function SetAuthority() {

    var arr_cbxs = document.getElementById("ul_Authority").getElementsByTagName("input");
    document.getElementById("hdf_SelectedAuthority").value = authorityItems;

    authorityItems = "," + authorityItems + ",";
    for (var i = 0; i < arr_cbxs.length; i++) {
        if (arr_cbxs[i].type == "checkbox") {

            if (authorityItems.indexOf("," + arr_cbxs[i].value + ",") > 0) {
                arr_cbxs[i].checked = "checked";
             }            
        }
    }

 }

function GetSelectedAuthority() {
    
    var arr_cbxs = document.getElementById("ul_Authority").getElementsByTagName("input");

    var value = "";

    for (var i = 0; i < arr_cbxs.length; i++) {
        if (arr_cbxs[i].type == "checkbox") {
            if (arr_cbxs[i].checked) {
                value += arr_cbxs[i].value + ",";
            }
        }
    }

    value = value.substring(0,value.length-1);

    document.getElementById("hdf_SelectedAuthority").value = value;
}

function init() {
    if (authorityItems == "") {
        BindAuthorityItems(false);
    } else {
        BindAuthorityItems(true);
    }
}
